//
//  RJPieCartView.m
//  AirQuality
//
//  Created by Po on 15/3/18.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJPieCartView.h"
#define K_PI 3.1415
#define KDGREED(x) ((x)  * K_PI * 2)
@implementation RJPieCartView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _titleRect = CGRectMake(0, 0, 0, 0);
        _spaceHeight = 20;
        _scaleY = 0.8;
        [self setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    CGFloat centerX = CGRectGetWidth(self.frame) / 4;
    CGFloat centerY = CGRectGetHeight(self.frame) / 2 + ((DeviceType < 5.5) ? 5 :10 );
    BOOL isNoData = NO;

    CGFloat radius = (centerX > centerY ? centerY : centerX) - ((DeviceType < 5.5) ? 20 : 10);
    //获取上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetAllowsAntialiasing(context, true);
    float sum = 0;
    for (NSUInteger i = 0; i < [_valueArr count]; i ++) {
        CGFloat value = [[_valueArr objectAtIndex:i] floatValue];
        sum += value;
    }
    
    if (sum == 0) {
        isNoData = YES;
        sum = 1;
    }
    
    CGContextMoveToPoint(context, centerX, centerY);
    float currentTangel = 0;
    
    //饼图
    CGContextSaveGState(context);
    CGContextScaleCTM(context, 1.0, _scaleY);

    for (NSUInteger i = 0; i < [_valueArr count]; i ++) {
        float startAngle = KDGREED(currentTangel);
        if (isNoData && i == 0) {
            currentTangel = 1.5;
        } else {
            currentTangel += [[_valueArr objectAtIndex:i] floatValue] /sum;
        }
        float endAngle = KDGREED(currentTangel);
        //绘制上方扇形
//        CGContextMoveToPoint(context, 80, centerY);
//        [[_colorArr objectAtIndex:i % [_valueArr count]] setFill];
//        [[UIColor colorWithWhite:1.0 alpha:0.8] setStroke];
//        CGContextAddArc(context, centerX, centerY, radius, startAngle, endAngle, 0);
//        CGContextClosePath(context);
//        CGContextDrawPath(context, kCGPathFill);
        
        //绘制侧面
        float starx = cos(startAngle) * radius + centerX;
        float stary = sin(startAngle) * radius + centerY;
//        float endx = cos(endAngle) * radius + centerX;
//        float endy = sin(endAngle) * radius + centerY;
        
//        float endy1 = endy + _spaceHeight;
        
        //弧度小于3.14才会画前面的厚度
        if (endAngle < K_PI) {
        } else if (startAngle < K_PI){
            endAngle = K_PI;
//            endx = 10;
//            endy1 = 230 + _spaceHeight;
        } else {
            continue;
        }
        //绘制厚度
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathMoveToPoint(path, nil, starx, stary);
        CGPathAddArc(path, nil, centerX, centerY, radius, startAngle, endAngle, 0);
//        CGPathAddLineToPoint(path, nil, endx, endy1);
        CGPathAddArc(path, nil, centerX, centerY + _spaceHeight, radius, endAngle, startAngle, 1);
        CGContextAddPath(context, path);
        [[_colorArr objectAtIndex:i % [_valueArr count]] setFill];
        [[UIColor colorWithWhite:0.9 alpha:1.0] setStroke];
        
        
        CGContextDrawPath(context, kCGPathFill);
        
        [[UIColor colorWithWhite:0.1 alpha:0.4] setFill];
        CGContextAddPath(context, path);
        CGContextDrawPath(context, kCGPathFill);
    }
    //整体渐变
    CGFloat componets[] = {0.0, 0.0, 0.0, 0.0,0.0,0.0,0.0,0.1};
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, componets, nil, 2);
    CGContextDrawRadialGradient(context, gradient, CGPointMake(centerX, centerY), 0, CGPointMake(centerX, centerY), radius, 0);
    CFRelease(colorspace);
    CGGradientRelease(gradient);
    CGContextRestoreGState(context);
    
    //绘制文字
    CGFloat wordX = _titleRect.origin.x + 10;
    CGFloat wordY = _titleRect.origin.y ;
    CGFloat wordHeight = (_titleRect.size.height) / ([_valueArr count] + 1);
    if (wordHeight > 30) {
        wordHeight = 30;
        wordY = (self.frame.size.height - wordHeight * _titleArr.count) / 2;
    }
//    if (wordHeight < 20) {
//        wordHeight = 20;
//    }
    
    for (NSUInteger i = 0; i < [_valueArr count]; i ++) {
        wordY += wordHeight - 5;

//        CGContextFillRect(context, CGRectMake(wordX, wordY, wordHeight - 10, wordHeight - 10));
        CAShapeLayer * rectLayer = [CAShapeLayer layer];
        [rectLayer setFrame:CGRectMake(wordX, wordY, wordHeight - 10, wordHeight - 10)];
        UIColor * color = [_colorArr objectAtIndex:i % [_valueArr count]];
        [rectLayer setBackgroundColor:color.CGColor];
        [rectLayer setCornerRadius:2];
        [self.layer addSublayer:rectLayer];
        CGContextDrawPath(context, kCGPathFill);
        if (i < [_titleArr count]) {
            NSString * title = [_titleArr objectAtIndex:i];
            [title drawAtPoint:CGPointMake(wordX + wordHeight, wordY) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:wordHeight - 14],NSForegroundColorAttributeName:[UIColor blackColor]}];
        }
    }
}


@end
